package com.app.micaihu.bean.news;

import android.text.Html;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.app.micaihu.R;
import com.app.micaihu.configure.Config;
import com.app.micaihu.configure.UmEventId;
import com.app.micaihu.custom.components.downandupload.LoadingView;
import com.app.micaihu.custom.view.CustomImageView;
import com.app.micaihu.utils.MobclickAgentUtils;
import com.app.micaihu.videoplayer.JCVideoPlayer;
import com.app.micaihu.videoplayer.JCVideoPlayerGame;
import com.app.utils.util.image.LoadImageUtils;
import com.baidu.mobstat.StatService;
import com.blankj.utilcode.util.Utils;
import com.facebook.drawee.generic.RoundingParams;

/**
 * 新闻列表的一张小图UI  关于游戏下载的
 * Created by linkai on 2017/6/08.
 */
public class ViewNewsGameOneSmall {

    public CustomImageView smallImg;
    public TextView titleView;
    public TextView descView;
    public LoadingView downView;

    private JCVideoPlayerGame videoPlayer;

    public ViewNewsGameOneSmall(View convertView) {
        initView(convertView);
    }


    /**
     * 初始化控件
     *
     * @param convertView
     */
    public void initView(View convertView) {
        this.videoPlayer = convertView.findViewById(R.id.videoPlayer);
        this.titleView = (TextView) convertView.findViewById(R.id.item_public_title);
        this.descView = (TextView) convertView.findViewById(R.id.item_public_desc);
        this.smallImg = (CustomImageView) convertView.findViewById(R.id.item_small_image);
        downView = (LoadingView) convertView.findViewById(R.id.item_public_download);
        downView.setEventData(UmEventId.INSTANCE.getSearchsuccess());
        smallImg.getHierarchy().setRoundingParams(new RoundingParams().setCornersRadius(28.0f));
        convertView.setTag(this);
    }

    /**
     * 加载数据
     *
     * @param newsEntity
     */
    public void loadData(NewsEntity newsEntity) {
        this.titleView.setText(newsEntity.getArticleTitle());
        this.descView.setText(newsEntity.getSlogan());
        LoadImageUtils.getInstance().loadWifiImage(this.smallImg, newsEntity.getAppIcon());

        downView.setTag(newsEntity);
    }

    /**
     * 加载数据
     *
     * @param newsEntity
     * @param matchStr
     */
    public void loadData(NewsEntity newsEntity, String matchStr, int position) {
//        MobclickAgentUtils.event(Config.EventBauduId.game_exposed, "搜索");
        StatService.onEvent(Utils.getApp().getApplicationContext(), "game_exposed", "搜索", 1);//统计事件的点击次数
        loadData(newsEntity);
        matchTitle(newsEntity.getArticleTitle(), matchStr);
        if (position == 0) {
            videoPlayer.setVisibility(View.VISIBLE);
        } else {
            videoPlayer.setVisibility(View.GONE);
        }
        String thumb = "";
        String[] articleThumb = newsEntity.getArticleThumb();
        if (articleThumb != null) {
            thumb = articleThumb[0];
        }
        videoPlayer.setmAutoDirection(false);
        //直接播放接口的Url
        videoPlayer.setUp(newsEntity.getVideoCurl(), JCVideoPlayer.SCREEN_LAYOUT_NORMAL,
                newsEntity.getArticleTitle(), thumb);
        ShareBean shareBean = new ShareBean();
        shareBean.setArticleId(newsEntity.getArticleId());
        videoPlayer.setShareBean(shareBean);
        videoPlayer.fullscreenButton.setVisibility(View.GONE);
        videoPlayer.startButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                videoPlayer.startVideo();
            }
        });

    }

    /**
     * 加载数据
     *
     * @param titleStr
     * @param matchStr 匹配的字符串
     */
    protected void matchTitle(String titleStr, String matchStr) {
        if (TextUtils.isEmpty(matchStr) || TextUtils.isEmpty(titleStr)) {
            return;
        }
        int startIndex = titleStr.toLowerCase().indexOf(matchStr.toLowerCase());
        if (startIndex >= 0) {
            //开始匹配红色字符
            int endIndex = startIndex + matchStr.length();
            String oneStr = titleStr.substring(0, startIndex);
            if (endIndex > titleStr.length() - 1) {
                endIndex = titleStr.length() - 1;
            }
            String twoStr = titleStr.substring(endIndex);
            matchStr = titleStr.substring(startIndex, endIndex);
            String str;
            str = oneStr + "<font color='#E55D5D'>" + matchStr + "</font>" + twoStr;
            this.titleView.setText(Html.fromHtml(str));
        }

    }
}
